package com.xoa.controller.document;

import com.xoa.dao.common.SysCodeMapper;
import com.xoa.dao.users.UsersMapper;
import com.xoa.model.common.SysCode;
import com.xoa.model.document.DocumentExchangeSet;
import com.xoa.model.users.Users;
import com.xoa.service.document.DocumentExchangeSetService;
import com.xoa.util.ToJson;
import com.xoa.util.page.PageParams;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/documentExchangeSet")
public class DocumentExchangeSetController {
    @Resource
    private DocumentExchangeSetService documentExchangeService;
    @Resource
    private SysCodeMapper sysCodeMapper;
    @Resource
    private UsersMapper usersMapper;

    //查询部门下用户
    @RequestMapping("/selectUsersByDeptId")
    @ResponseBody
    public ToJson<Users> selectUsersByDeptId(Integer deptId) {
        ToJson<Users> tojson = new ToJson<Users>();
        List<Users> users = usersMapper.selectUserNameByDeptId(deptId);
        if (users != null) {
            tojson.setObj(users);
            tojson.setMsg("ok");
            tojson.setFlag(0);
        } else {
            tojson.setMsg("err");
            tojson.setFlag(1);
        }
        return tojson;
    }

    //处理时间  将前台传过来的时间存到数据库中
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        sdf.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }

    //查询所有数据
    @RequestMapping("/selectDocumentExchange")
    @ResponseBody
    public ToJson<DocumentExchangeSet> selectDocumentExchange(DocumentExchangeSet documentExchangeSet, PageParams pageParams, Integer limit) {
        pageParams.setPageSize(limit);
        pageParams.setUseFlag(true);
        return documentExchangeService.selectDocumentExchange(documentExchangeSet, pageParams);
    }

    //根据权限查询数据
    @RequestMapping("/selectByDocumentPriv")
    @ResponseBody
    public ToJson<DocumentExchangeSet> selectByDocumentPriv(DocumentExchangeSet documentExchangeSet) {
        return documentExchangeService.selectByDocumentPriv(documentExchangeSet);
    }

    //新添加数据
    @RequestMapping("/insertDocumentExchange")
    @ResponseBody
    public ToJson<DocumentExchangeSet> insertDocumentExchange(HttpServletRequest request, DocumentExchangeSet documentExchangeSet) {
        ToJson<DocumentExchangeSet> tojson = new ToJson<DocumentExchangeSet>();
        int result = documentExchangeService.saveDocumentExchange(request, documentExchangeSet);
        if (result == 1) {
            tojson.setMsg("添加成功");
            tojson.setFlag(0);
        } else {
            tojson.setMsg("添加失败");
            tojson.setFlag(1);
        }
        return tojson;
    }

    //更新一条
    @RequestMapping("/updateDocumentExchange")
    @ResponseBody
    public ToJson<DocumentExchangeSet> updateDocumentExchange(DocumentExchangeSet documentExchangeSet) {
        ToJson<DocumentExchangeSet> tojson = new ToJson<DocumentExchangeSet>();
        int result = documentExchangeService.updateDocumentExchange(documentExchangeSet);
        if (result == 1) {
            tojson.setMsg("更新成功");
            tojson.setFlag(0);
        } else {
            tojson.setMsg("更新失败");
            tojson.setFlag(1);
        }
        return tojson;
    }

    //根据ID回显数据
    @RequestMapping("/selectDocumentExchangeById")
    @ResponseBody
    public ToJson<DocumentExchangeSet> getRuleById(Integer exchangeId) {
        ToJson<DocumentExchangeSet> tojson = new ToJson<DocumentExchangeSet>();
        DocumentExchangeSet documentExchangeSet = documentExchangeService.selectDocumentExchangeById(exchangeId);
        if (documentExchangeSet != null) {
            tojson.setObject(documentExchangeSet);
            tojson.setMsg("ok");
            tojson.setFlag(0);
        }
        return tojson;
    }

    //删除一条
    @RequestMapping("/deleteDocumentExchange")
    @ResponseBody
    public ToJson<DocumentExchangeSet> deleteDocumentExchange(Integer exchangeId) {
        ToJson<DocumentExchangeSet> tojson = new ToJson<DocumentExchangeSet>();
        int result = documentExchangeService.deleteDocumentExchange(exchangeId);
        if (result == 1) {
            tojson.setMsg("删除成功");
            tojson.setFlag(0);
        } else {
            tojson.setMsg("删除失败");
            tojson.setFlag(1);
        }
        return tojson;
    }

    /**
     * 查询单位类型
     */
    @RequestMapping("selectByType")
    @ResponseBody
    public ToJson<Integer> selectByType(HttpServletRequest request) {
        ToJson json = new ToJson<Integer>(1, "err");
        String sysCode = request.getParameter("SysCode");
        try {
            List<SysCode> sysCodes = sysCodeMapper.getSysCode(sysCode);
            if (sysCodes != null) {
                json.setObj(sysCodes);
                json.setFlag(0);
                json.setMsg("true");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    // 查看是否有发文权限
    @ResponseBody
    @RequestMapping("/selectByUser")
    public ToJson selectByUser(HttpServletRequest request) {
        return documentExchangeService.selectByUser(request);
    }
}
